import {Component, EventEmitter,Input, OnInit, Output} from "@angular/core";
import {NzNotificationService} from "ng-zorro-antd";
import {HttpServcie} from "../../../service/http.service";
import {SpotObjectPlan} from "../../../pages/maintenancePlan/bean/spotObjectPlan";
import {SpotCheck} from "../../../pages/maintenancePlan/spot-check/spot-check";



@Component({
  selector: "spot-object-add",
  templateUrl: "spot-object-add.html",
  styleUrls: ["spot-object-add.scss"]
})
export class SpotObjectAdd implements OnInit {

  @Input()
  inValue: any = {};

  // 定义父子传值事件
  @Output("outPut")
  public outPut = new EventEmitter();
  // 定义对象
  public spotObjectPlan:SpotObjectPlan = new SpotObjectPlan();
  // 定义当前登录人的信息
  public user:any;
  // 定义实施人和责任人的信息
  public objects:any=[];
  // 定义实施对象选中数据
  public objectList:any=[];
  // 定义计划名称
  public planNameEn:any;
  // 定义计划实体信息
  public planData:any;

  constructor(
    private potCheck:SpotCheck,
    private _notification: NzNotificationService,
    private httpServcie: HttpServcie
  ) {
  }

  ngOnInit() {
    this.user = JSON.parse(window.localStorage.getItem("user"));
    this.planData = this.inValue.clumeData;
    this.planNameEn = this.inValue.clumeData.planName;
    this.queryMachine();
  }

  // 添加方法
  submitForm($event) {

    if (this.objectList.length <= 0 || this.objectList == null) {
      this._notification.create("warning", "警告提示", "实施对象不能为空！");
      return false;
    } else {
      this.spotObjectPlan.implementObjectId = this.objectList.join(",");
    }

    if(this.spotObjectPlan.headMan ==undefined ){
      this._notification.create("warning", "警告提示", "负责人不能为空！");
      return false;
    }
    // 创建人
    this.spotObjectPlan.createBy = JSON.parse(window.localStorage.getItem("user")).userName;
    this.spotObjectPlan.planId = this.planData.id;
    this.spotObjectPlan.userId = this.user.id;
    console.log(this.spotObjectPlan);
    this.httpServcie.insertSpotObjectPlans(this.spotObjectPlan).subscribe(data => {
        if (data.status == 200) {
          this._notification.create("success", "成功", "新增成功！");
          this.outPut.emit($event);
        } else {
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        this._notification.create("error", "错误提示", error);
      }
    )
  }

  // 根据当前得维保单位查询维保人员
  queryMachine() {

    this.httpServcie.queryUserByPartyId(this.user.partyId).subscribe(data => {
      if (data.status == 200) {
        if (data.data && data.data.length > 0) {

          console.log(data.data);
          this.objects = data.data;
        }
      }
    });
  }


}
